using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using UMC.Data;
using System.IO;

namespace UMC.WorkFlow.Subassembly
{
    /// <summary>
    /// 流程线路
    /// </summary>
    [UIDesign(Name = "方向线", Explain = "指定审批流向方向")]
    public class Line : UMC.WorkFlow.Line
    {
        public override void Activing(UMC.WorkFlow.Shape shape, UMC.WorkFlow.TaskInstance task, TaskRuntime runtime)
        {
            if (this.IsMust == false)
            {
                if (this.SQL != null && String.IsNullOrEmpty(this.SQL.DbProviderKey) == false && String.IsNullOrEmpty(this.SQL.SqlText) == false)
                {

                    IDictionary dic = new Hashtable(runtime.Items);
                    UMC.WorkFlow.Shape shp = task.Shapes.Find(sp => sp.Id == this.ToId);
                    if (shp.Identity != null && String.IsNullOrEmpty(shp.Identity.Username) == false)
                    {
                        dic.Add("To.UserName", shp.Identity.Username);
                    }
                    object obj = UMC.WorkFlow.Subassembly.SubassemblyHelper.ExecuteScalar(this.SQL, dic);
                    if (obj != null)
                    {
                        if (Utility.IntParse(obj.ToString(), 0) >= 0)
                        {
                            this.Status = UMC.WorkFlow.LineStatus.Destructive;

                        }


                    }
                }
            }


        }
        [DesignAttribute(Caption = "阻断脚本", Explain = "当第一行第一列的值大于0，则阻断此路线")]
        public SqlSetting SQL { get; set; }
        public override Func<object> GetInstance(string prototypeName)
        {
            switch (prototypeName)
            {
                case "SQL":
                    return () => new SqlSetting();
                default:
                    return base.GetInstance(prototypeName);
            }
        }
        protected override void WriteProperty(TextWriter writer)
        {
            if (SQL != null)
            {
                writer.Write(',');
                JSON.Serialize("SQL", this.SQL, writer);
            }
            base.WriteProperty(writer);
        }
        public override void Read(string key, object value)
        {
            switch (key)
            {
                case "SQL":
                    this.SQL = (SqlSetting)value;
                    break;
                default:
                    base.Read(key, value);
                    break;
            }
        }
        // override 
    }
}
